package com.example.controller;

import com.example.pojo.Vip;
import com.example.pojo.commodity.Shopping;
import com.example.service.VipService;
import com.example.service.commodity.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author LinJie
 * @create 2020-12-11 14:24
 */
@Controller
@RequestMapping("/vip")
public class VipController {

    @Autowired
    private VipService vipService;
    @Autowired
    private ShoppingService shoppingService;

    @RequestMapping("/queryAll")
    public String queryAll(Model model) {
        List<Vip> vips = vipService.queryAll();
        System.out.println("【vip】" + vips);
        model.addAttribute("allVips", vips);
        return "/vip/vip-tables2";
    }

/*    @RequestMapping("/toInsertVip")
    public String toInsertVip() {
        return "/vip/toInsertVip";
    }*/

    @RequestMapping("/insertVip")
    public String insertVip(Vip vip, Model model) {
        int flag = vipService.addVip(vip);
        if (flag == 1) {
            List<Vip> vips = vipService.queryAll();
            model.addAttribute("allVips", vips);
            model.addAttribute("allVips", vips);
            model.addAttribute("addmsg", "添加成功");
            return "/vip/vip-tables2";
        } else {
            model.addAttribute("addmsg", "编号已存在，请更改");
            return "/vip/toInsertVip";
        }

    }

    @RequestMapping("/toUpdateVip/{vip_id}")
    public String toUpdateVip(Model model, @PathVariable int vip_id) {
        Vip vip = vipService.queryVip(vip_id);
        System.out.println(vip);
        model.addAttribute("vip", vip);
        return "/vip/toUpdateVip";
    }

    @RequestMapping("/updateVip")
    public String updateVip(Vip vip) {
        System.out.println(vip);
        vipService.updateVip(vip);
        return "redirect:/vip/queryAll";
    }

    @RequestMapping("/deleteVip/{vip_id}")
    public String deleteVip(@PathVariable int vip_id) {
        System.out.println(vip_id);
        vipService.deleteVip(vip_id);
        return "redirect:/vip/queryAll";
    }

    @RequestMapping("/verifyVip")
    public String verifyVip(@RequestParam("userId") String userId,
                            @RequestParam("telephone") String telephone,
                            Model model, HttpSession session) {
        int total = 0;
        double totallin = 0;
        List<Shopping> shoppings = shoppingService.queryAll();
        for (Shopping shopping : shoppings) {
            total += shopping.getProduct_total();
        }
        model.addAttribute("shoppingCar", shoppings);
        model.addAttribute("total", total);
        if (userId.equals("") || telephone.equals("")) {
            model.addAttribute("msg", "请填写完整的会员信息");
            return "/product/shoppingcar";
        }
        System.out.println("会员id" + userId);
        System.out.println("手机号" + telephone);
        int flag = vipService.verifyVip(userId, telephone);
        double discount = 0;
        if (flag == 1) {
            model.addAttribute("msg", "会员信息存在,可以打折");
            model.addAttribute("isVip", 1);      //会员
            for (Shopping shopping : shoppings) {
                totallin += shopping.getProduct_total();           //计算折后价
            }
            discount = 0.9 * totallin;
            model.addAttribute("discount", discount);
            return "/product/shoppingcar";
        } else if (flag == -1) {
            model.addAttribute("msg", "会员编号出错");
            model.addAttribute("isVip", 0);      //非会员
            return "/product/shoppingcar";
        } else if (flag == -2) {
            model.addAttribute("msg", "会员电话号码出错");
            model.addAttribute("isVip", 0);      //非会员
            return "/product/shoppingcar";
        } else {
            System.out.println(flag);
            model.addAttribute("msg", "没有该会员信息");
            model.addAttribute("isVip", 0);      //非会员
            return "/product/shoppingcar";
        }
    }

}
